class Game {

    String gameID
    Player redPlayer
    Player blackPlayer
    List rounds = []
    //Board board
    List pieces = []
    static hasMany = [rounds: Round, pieces: Piece]
    static constraints = {
    }

    def Game() {
        initPieces()
    }

    def getNextPlayer() {
        log.info "game ${this.gameID}, round ${this.rounds.size()}"
        if (this.rounds.size() > 0) {
            def round = this.rounds[-1]
            if (round.blackMove == null) {
                log.info "blackMove"
                return Piece.cBlack
            } else {
                log.info "redMove"
                return Piece.cRed
            }
        } else {
            log.info "new game"
            return Piece.cRed
        }

    }

    def getMaxRound() {
        return rounds.size()
    }

    def addMove(Move newMove) {
        log.info "piece move: " + newMove
        def piece = this.pieces.findAll {it ->
            if (it.x == newMove.moveFromX &
                    it.y == newMove.moveFromY &
                    it.alive == true) {
                return it
            }
        }
        log.info "piece found" + piece
        if (piece[0].x == newMove.moveToX & piece[0].y == newMove.moveToY) {
            return false
        }
        this.pieces.each {it ->
            if (it.x == newMove.moveToX &
                    it.y == newMove.moveToY) {
                it.alive = false
                log.info it 
            }
        }
        piece[0].x = newMove.moveToX
        piece[0].y = newMove.moveToY
        return true
    }

    def getBoard() {
        return this.pieces.findAll{ it->
            it.alive ==  true
        }
     }

    def initPieces() {
        def piece = new Piece(Piece.tJu, Piece.cRed, 1, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tJu, Piece.cRed, 9, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tMa, Piece.cRed, 2, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tMa, Piece.cRed, 8, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tXiang, Piece.cRed, 3, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tXiang, Piece.cRed, 7, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tShi, Piece.cRed, 4, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tShi, Piece.cRed, 6, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tJiang, Piece.cRed, 5, 1)
        this.pieces.add(piece)
        piece = new Piece(Piece.tPao, Piece.cRed, 2, 3)
        this.pieces.add(piece)
        piece = new Piece(Piece.tPao, Piece.cRed, 8, 3)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cRed, 1, 4)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cRed, 3, 4)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cRed, 5, 4)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cRed, 7, 4)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cRed, 9, 4)
        this.pieces.add(piece)
        piece = new Piece(Piece.tJu, Piece.cBlack, 1, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tJu, Piece.cBlack, 9, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tMa, Piece.cBlack, 2, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tMa, Piece.cBlack, 8, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tXiang, Piece.cBlack, 3, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tXiang, Piece.cBlack, 7, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tShi, Piece.cBlack, 4, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tShi, Piece.cBlack, 6, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tJiang, Piece.cBlack, 5, 10)
        this.pieces.add(piece)
        piece = new Piece(Piece.tPao, Piece.cBlack, 2, 8)
        this.pieces.add(piece)
        piece = new Piece(Piece.tPao, Piece.cBlack, 8, 8)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cBlack, 1, 7)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cBlack, 3, 7)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cBlack, 5, 7)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cBlack, 7, 7)
        this.pieces.add(piece)
        piece = new Piece(Piece.tBin, Piece.cBlack, 9, 7)
        this.pieces.add(piece)

    }
}
